/**
 * 
 */
package com.ydy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ydy.annotation.CheckFormRepeat;
import com.ydy.annotation.CtrlParam;
import com.ydy.annotation.StaffToken;
import com.ydy.controller.base.BaseController;
import com.ydy.model.Sku;
import com.ydy.model.Spu;
import com.ydy.service.good.GoodService;
import com.ydy.utils.StringUtils;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.other.PageVo;

/**
 * @author xuzhaojie
 *
 *         2019年1月18日 上午9:17:29
 */
@Controller
@RequestMapping(value = "good", name = "商品模块")
public class GoodController extends BaseController {

	@Autowired
	private GoodService goodService;

	@GetMapping(value = "listSpu", name = "用户查看商品列表")
	@ResponseBody
	public ResponseEntity<PageVo<Spu>> listSpu(String name, @CtrlParam("店铺id") Long shopId, Integer page,
			Integer size) {
		PageVo<Spu> vo = goodService.list(name, shopId, page, size);
		return ResponseEntity.ok(vo);
	}

	@GetMapping(value = "spuDetail", name = "用户查看商品详情")
	@ResponseBody
	public ResponseEntity<Spu> spuDetail(@CtrlParam("商品ID") Long spuId) {
		return ResponseEntity.ok(goodService.selectSpuByIdForUser(spuId));
	}

	@StaffToken
	@GetMapping(value = "selectSpu", name = "店员查看商品列表")
	@ResponseBody
	public ResponseEntity<PageVo<Spu>> selectSpu(Spu spu, Integer page, Integer size) {
		StringUtils.setParamEmptyToNull(spu);
		PageVo<Spu> vo = goodService.select(spu, getStaff(), page, size);
		return ResponseEntity.ok(vo);
	}

	@StaffToken
	@GetMapping(value = "selectSpuById", name = "店员查看商品详情")
	@ResponseBody
	public ResponseEntity<Spu> selectSpuById(@CtrlParam("商品ID") Long spuId) {
		return ResponseEntity.ok(goodService.selectSpuById(spuId, getStaff()));
	}

	@StaffToken
	@GetMapping(value = "selectSkuById", name = "店员查看子商品详情")
	@ResponseBody
	public ResponseEntity<Sku> selectSkuById(@CtrlParam("skuId") Long skuId) {
		return ResponseEntity.ok(goodService.selectSkuById(skuId, getStaff()));
	}

	@CheckFormRepeat
	@StaffToken
	@PostMapping(value = "saveSpu", name = "店员保存商品信息")
	@ResponseBody
	public ResponseEntity<Spu> saveSpu(Spu spu) {
		StringUtils.setParamEmptyToNull(spu);
		spu = goodService.saveOrUpdateSpu(spu, getStaff());
		return ResponseEntity.ok(spu);
	}

	@StaffToken
	@DeleteMapping(value = "deleteSpu", name = "店员删除商品")
	@ResponseBody
	public ResponseEntity<BaseVo> deleteSpu(@CtrlParam("spuId") Long spuId) {
		return ResponseEntity.ok(goodService.deleteSpu(spuId, getStaff()));
	}

	@StaffToken
	@PostMapping(value = "statusSpu", name = "店员上下架商品")
	@ResponseBody
	public ResponseEntity<BaseVo> statusSpu(@CtrlParam("spuId") Long spuId) {
		return ResponseEntity.ok(goodService.statusSpu(spuId, getStaff()));
	}

	@StaffToken
	@PostMapping(value = "upSpu", name = "店员置顶商品")
	@ResponseBody
	public ResponseEntity<BaseVo> topSpu(@CtrlParam("spuId") Long spuId) {
		return ResponseEntity.ok(goodService.upSpu(spuId, getStaff()));
	}

	@CheckFormRepeat
	@StaffToken
	@PostMapping(value = "saveSku", name = "店员保存子商品")
	@ResponseBody
	public ResponseEntity<Sku> saveSku(Sku sku) {
		StringUtils.setParamEmptyToNull(sku);
		sku = goodService.saveOrUpdateSku(sku, getStaff());
		return ResponseEntity.ok(sku);
	}

	@StaffToken
	@GetMapping(value = "selectSkuBySpuId", name = "店员筛选商品下的子商品")
	@ResponseBody
	public ResponseEntity<List<Sku>> selectSkuBySpuId(@CtrlParam("spuId") Long spuId) {
		return ResponseEntity.ok(goodService.selectSkuBySpuId(spuId, getStaff()));
	}

	@StaffToken
	@DeleteMapping(value = "deleteSku", name = "店员删除子商品")
	@ResponseBody
	public ResponseEntity<BaseVo> deleteSku(@CtrlParam("skuId") Long skuId) {
		return ResponseEntity.ok(goodService.deleteSku(skuId, getStaff()));
	}

	@StaffToken
	@PostMapping(value = "statusSku", name = "店员上下架子商品")
	@ResponseBody
	public ResponseEntity<BaseVo> statusSku(@CtrlParam("skuId") Long skuId) {
		return ResponseEntity.ok(goodService.statusSku(skuId, getStaff()));
	}

}